package com.leyikao.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leyikao.util.HttpRequest;
import com.leyikao.util.ToolUtil;

/**
 * 简历中心控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/resume")
public class ResumeCtrl {
	
	/**
	 * 转跳简历中心
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	public String ent_resume(HttpServletRequest request,Model model){
		HttpRequest hreq = new HttpRequest();
		//从session获取用户信息
		String enterpriseInfo = request.getSession().getAttribute("enterpriseInfo").toString();
		JSONObject enterpriseObject = JSONObject.fromObject(enterpriseInfo);
		Map<String, Object> enterpriseInfoMap = JSONObject.fromObject(enterpriseObject);
		String userID = enterpriseInfoMap.get("uid").toString();//企业ID
		int pageNo1 = 1;
		int pageNo2 = 1;
		int pageNo3 = 1;
		int flag=0;
		if(ToolUtil.strNotNull(request.getParameter("rpage"))){
			pageNo1 = Integer.parseInt(request.getParameter("rpage"));
			flag = 1;
		}
		if(ToolUtil.strNotNull(request.getParameter("cpage"))){
			pageNo2 = Integer.parseInt(request.getParameter("cpage"));
			flag = 2;
		}
		if(ToolUtil.strNotNull(request.getParameter("dpage"))){
			pageNo3 = Integer.parseInt(request.getParameter("dpage"));
			flag = 3;
		}
		String type="1";//列表类型：1 收到的;2 收藏的; 3 已下载的
		String params1 = "\"ent_resume_list\",\"ent_user_id\":\""+userID+"\",\"list_type\":\""+type+"\",\"page\":\""+pageNo1+"\"";
		String params2 = "\"ent_resume_list\",\"ent_user_id\":\""+userID+"\",\"list_type\":\"2\",\"page\":\""+pageNo2+"\"";
		String params3 = "\"ent_resume_list\",\"ent_user_id\":\""+userID+"\",\"list_type\":\"3\",\"page\":\""+pageNo3+"\"";
		String json1 = hreq.post(params1);
		String json2 = hreq.post(params2);
		String json3 = hreq.post(params3);
		JSONObject jsonObject1 = JSONObject.fromObject(json1);
    	Map<String, Object> mapJson1 = JSONObject.fromObject(jsonObject1);
    	JSONObject jsonObject2 = JSONObject.fromObject(json2);
    	Map<String, Object> mapJson2 = JSONObject.fromObject(jsonObject2);
    	JSONObject jsonObject3 = JSONObject.fromObject(json3);
    	Map<String, Object> mapJson3 = JSONObject.fromObject(jsonObject3);
		model.addAttribute("WebSite", "E朝朝_简历")
			 .addAttribute("nav_tag","简历")
			 .addAttribute("received_list",mapJson1.get("data"))
			 .addAttribute("received_list_result",mapJson1.get("result"))	
			 .addAttribute("received_list_message",mapJson1.get("message"))	
			 .addAttribute("rpage",pageNo1)
			 .addAttribute("collection_list",mapJson2.get("data"))
			 .addAttribute("collection_list_result",mapJson2.get("result"))	
			 .addAttribute("collection_list_message",mapJson2.get("message"))
			 .addAttribute("cpage",pageNo2)
			 .addAttribute("download_list",mapJson3.get("data"))
			 .addAttribute("download_list_result",mapJson3.get("result"))	
			 .addAttribute("download_list_message",mapJson3.get("message"))
			 .addAttribute("dpage",pageNo3)
			 .addAttribute("dataFlag",flag);
		return "cvcenter/ent_resume";
	}
	
	/**
	 * 查看简历详情
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/r-{id}.html")
	public String resumeDeatil(@PathVariable("id") String id,HttpServletRequest request,Model model){
		HttpRequest hreq = new HttpRequest();
		//从session获取用户信息
		String enterpriseInfo = request.getSession().getAttribute("enterpriseInfo").toString();
		JSONObject enterpriseObject = JSONObject.fromObject(enterpriseInfo);
		Map<String, Object> enterpriseInfoMap = JSONObject.fromObject(enterpriseObject);
		String userID = enterpriseInfoMap.get("uid").toString();//企业ID
		String itps = request.getParameter("internships");//工作经验
		String params = "\"ent_download_count\",\"ent_user_id\":\""+userID+"\"";//下载次数接口
		String params1 = "\"stu_resume_user_info_view\",\"rid\":\""+id+"\"";
		String params2 = "\"stu_resume_job_objective_view\",\"rid\":\""+id+"\"";
		String params3 = "\"stu_resume_degree_list\",\"rid\":\""+id+"\"";
		String params4 = "\"stu_resume_interest_view\",\"rid\":\""+id+"\"";
		String params5 = "\"stu_resume_internships_list\",\"rid\":\""+id+"\"";
		String params6 = "\"stu_resume_self_appraisal_view\",\"rid\":\""+id+"\"";
		String params7 = "\"stu_resume_reward_view\",\"rid\":\""+id+"\"";
		String params8 = "\"stu_resume_certify_list\",\"rid\":\""+id+"\"";
		String params9 = "\"stu_resume_status\",\"ent_user_id\":\""+userID+"\",\"rid\":\""+id+"\"";
		
		//简历状态
		String status = hreq.post(params9);
		JSONObject statusObject = JSONObject.fromObject(status);
    	Map<String, Object> statusJson = JSONObject.fromObject(statusObject);
		
		//获取企业当日下载量
		String download = hreq.post(params);
		JSONObject jsonObject = JSONObject.fromObject(download);
    	Map<String, Object> downloadJson = JSONObject.fromObject(jsonObject);
    	
    	//个人基本信息
    	String base1 = hreq.post(params1);
		JSONObject baseObject = JSONObject.fromObject(base1);
    	Map<String, Object> baseJson = JSONObject.fromObject(baseObject);
    	
    	//求职意向
    	String intention = hreq.post(params2);
		JSONObject intentionObject = JSONObject.fromObject(intention);
    	Map<String, Object> intentionJson = JSONObject.fromObject(intentionObject);
    	String temp = intentionJson.get("data").toString().substring(1, intentionJson.get("data").toString().length()-1);
    	JSONObject tempObject = JSONObject.fromObject(temp);
    	Map<String, Object> tempJson = JSONObject.fromObject(tempObject);
    	String jobID = tempJson.get("job_type_id").toString();
    	//教育经历
    	String edu = hreq.post(params3);
		JSONObject eduObject = JSONObject.fromObject(edu);
    	Map<String, Object> eduJson = JSONObject.fromObject(eduObject);
    	
    	//兴趣特长
    	String interest = hreq.post(params4);
		JSONObject interestObject = JSONObject.fromObject(interest);
    	Map<String, Object> interestJson = JSONObject.fromObject(interestObject);
    	
    	//实习经验
    	String internships = hreq.post(params5);
		JSONObject internshipsObject = JSONObject.fromObject(internships);
    	Map<String, Object> internshipstJson = JSONObject.fromObject(internshipsObject);
    	
    	//自我评价
    	String appraisal = hreq.post(params6);
		JSONObject appraisalObject = JSONObject.fromObject(appraisal);
    	Map<String, Object> appraisalJson = JSONObject.fromObject(appraisalObject);
    	
    	//个人荣誉
    	String reward = hreq.post(params7);
		JSONObject rewardObject = JSONObject.fromObject(reward);
    	Map<String, Object> rewardJson = JSONObject.fromObject(rewardObject);
    	
    	//求职证书
    	String certify = hreq.post(params8);
		JSONObject certifyObject = JSONObject.fromObject(certify);
    	Map<String, Object> certifyJson = JSONObject.fromObject(certifyObject);
    	
    	model.addAttribute("WebSite", "E朝朝_简历")
		 .addAttribute("nav_tag","简历")
		 .addAttribute("stu_resume_id",id)
		 .addAttribute("job_id",jobID)
		 .addAttribute("itps",itps)
		 .addAttribute("download_count",downloadJson.get("data"))
		 .addAttribute("baseinfo", baseJson.get("data"))
		 .addAttribute("intention", intentionJson.get("data"))
		 .addAttribute("education", eduJson.get("data"))
    	.addAttribute("interest", interestJson.get("data"))
    	.addAttribute("internships", internshipstJson.get("data"))
    	.addAttribute("appraisal", appraisalJson.get("data"))
    	.addAttribute("reward", rewardJson.get("data"))
    	.addAttribute("certify", certifyJson.get("data"))
    	.addAttribute("status", statusJson.get("data"));
		return "cvcenter/ent_resumedetail";
	}
	
	/**
	 * 设置简历下载
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/download")
	public Map<String,Object> setDownload(HttpServletRequest request,Model model){
		Map<String,Object> map = new HashMap<String,Object>();
		HttpRequest hreq = new HttpRequest();
		String resumeID = request.getParameter("resumeID");//简历ID
		String jobID = request.getParameter("jobID");//职位ID
		String enterpriseInfo = request.getSession().getAttribute("enterpriseInfo").toString();
		JSONObject enterpriseObject = JSONObject.fromObject(enterpriseInfo);
		Map<String, Object> enterpriseInfoMap = JSONObject.fromObject(enterpriseObject);
		String userID = enterpriseInfoMap.get("uid").toString();//企业ID
		String params = "\"ent_resume_download\",\"ent_user_id\":\""+userID+"\",\"ent_job_id\":\""+jobID+"\",\"resume_id\":\""+resumeID+"\"";
		//个人荣誉
    	String json = hreq.post(params);
		JSONObject jsonObject = JSONObject.fromObject(json);
    	Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
    	int result = Integer.parseInt(mapJson.get("result").toString());
    	if(result > 0){
    		map.put("success", true);
    		map.put("count", mapJson.get("data"));
    	}else{
    		map.put("success", false);
    		map.put("message", mapJson.get("message"));
    	}
		return map;
	}
	
	/**
	 * 设置简历收藏
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/favorties")
	public Map<String,Object> setFavorites(HttpServletRequest request,Model model){
		Map<String,Object> map = new HashMap<String,Object>();
		HttpRequest hreq = new HttpRequest();
		String resumeID = request.getParameter("resumeID");//简历ID
		String enterpriseInfo = request.getSession().getAttribute("enterpriseInfo").toString();
		JSONObject enterpriseObject = JSONObject.fromObject(enterpriseInfo);
		Map<String, Object> enterpriseInfoMap = JSONObject.fromObject(enterpriseObject);
		String userID = enterpriseInfoMap.get("uid").toString();//企业ID
		String type = request.getParameter("action_type");//1添加收藏，2取消收藏
		String params = "\"ent_resume_favorites\",\"ent_user_id\":\""+userID+"\",\"resume_ids\":\""+resumeID+"\",\"action_type\":\""+type+"\"";
		//个人荣誉
    	String json = hreq.post(params);
		JSONObject jsonObject = JSONObject.fromObject(json);
    	Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
    	int result = Integer.parseInt(mapJson.get("result").toString());
    	if(result > 0){
    		map.put("success", true);
    		map.put("count", mapJson.get("data"));
    	}else{
    		map.put("success", false);
    		map.put("message", mapJson.get("message"));
    	}
		return map;
	}
}
